import request from "@/utils/request2"
import dayjs  from "dayjs"

const weekdays = ['星期日', '星期一', '星期二', '星期三', '星期四', '星期五', '星期六']

// Define the description and icon of the weather
const weathers = {
    CLEAR_DAY: { description: '晴（白天）', icon: '#icon-qing' },
    CLEAR_NIGHT: { description: '晴（夜间）', icon: '#icon-qing' },
    PARTLY_CLOUDY_DAY: { description: '多云（白天）', icon: '#icon-yun' },
    PARTLY_CLOUDY_NIGHT: { description: '多云（夜间）', icon: '#icon-yun' },
    CLOUDY: { description: '阴', icon: '#icon-yin' },
    LIGHT_HAZE: { description: '轻度雾霾', icon: '#icon-unknown' },
    MODERATE_HAZE: { description: '中度雾霾', icon: '#icon-unknown' },
    HEAVY_HAZE: { description: '重度雾霾', icon: '#icon-unknown' },
    LIGHT_RAIN: { description: '小雨', icon: '#icon-yu' },
    MODERATE_RAIN: { description: '中雨', icon: '#icon-yu' },
    HEAVY_RAIN: { description: '大雨', icon: '#icon-yu' },
    STORM_RAIN: { description: '暴雨', icon: '#icon-yu' },
    FOG: { description: '雾', icon: '#icon-unknown' },
    LIGHT_SNOW: { description: '小雪', icon: '#icon-unknown' },
    MODERATE_SNOW: { description: '中雪', icon: '#icon-unknown' },
    HEAVY_SNOW: { description: '大雪', icon: '#icon-unknown' },
    STORM_SNOW: { description: '暴雪', icon: '#icon-unknown' },
    DUST: { description: '浮尘', icon: '#icon-unknown' },
    SAND: { description: '沙尘', icon: '#icon-unknown' },
    WIND: { description: '大风', icon: '#icon-unknown' }
}

// Define the wind force levels and their corresponding wind speed ranges
const windForceLevels = [
    { level: 0, speedRange: { min: 0, max: 1 }, description: '无风' },
    { level: 1, speedRange: { min: 1, max: 5 }, description: '微风徐徐' },
    { level: 2, speedRange: { min: 6, max: 11 }, description: '清风' },
    { level: 3, speedRange: { min: 12, max: 19 }, description: '树叶摇摆' },
    { level: 4, speedRange: { min: 20, max: 28 }, description: '树枝摇动' },
    { level: 5, speedRange: { min: 29, max: 38 }, description: '风力强劲' },
    { level: 6, speedRange: { min: 39, max: 49 }, description: '风力强劲' },
    { level: 7, speedRange: { min: 50, max: 61 }, description: '风力超强' },
    { level: 8, speedRange: { min: 62, max: 74 }, description: '狂风大作' },
    { level: 9, speedRange: { min: 75, max: 88 }, description: '狂风呼啸' },
    { level: 10, speedRange: { min: 89, max: 102 }, description: '暴风毁树' },
    { level: 11, speedRange: { min: 103, max: 117 }, description: '暴风毁树' },
    { level: 12, speedRange: { min: 118, max: 133 }, description: '飓风' },
    { level: 13, speedRange: { min: 134, max: 149 }, description: '台风' },
    { level: 14, speedRange: { min: 150, max: 166 }, description: '强台风' },
    { level: 15, speedRange: { min: 167, max: 183 }, description: '强台风' },
    { level: 16, speedRange: { min: 184, max: 201 }, description: '超强台风' },
    { level: 17, speedRange: { min: 202, max: 220 }, description: '超强台风' }
]

const windDirections = [
    { direction: '北', symbol: 'N', centerAngle: 0, range: [348.76, 11.25] },
    { direction: '北东北', symbol: 'NNE', centerAngle: 22.5, range: [11.26, 33.75] },
    { direction: '东北', symbol: 'NE', centerAngle: 45, range: [33.76, 56.25] },
    { direction: '东东北', symbol: 'ENE', centerAngle: 67.5, range: [56.26, 78.75] },
    { direction: '东', symbol: 'E', centerAngle: 90, range: [78.76, 101.25] },
    { direction: '东东南', symbol: 'ESE', centerAngle: 112.5, range: [101.26, 123.75] },
    { direction: '东南', symbol: 'SE', centerAngle: 135, range: [123.76, 146.25] },
    { direction: '南东南', symbol: 'SSE', centerAngle: 157.5, range: [146.26, 168.75] },
    { direction: '南', symbol: 'S', centerAngle: 180, range: [168.76, 191.25] },
    { direction: '南西南', symbol: 'SSW', centerAngle: 202.5, range: [191.26, 213.75] },
    { direction: '西南', symbol: 'SW', centerAngle: 225, range: [213.76, 236.25] },
    { direction: '西西南', symbol: 'WSW', centerAngle: 247.5, range: [236.26, 258.75] },
    { direction: '西', symbol: 'W', centerAngle: 270, range: [258.76, 281.25] },
    { direction: '西西北', symbol: 'WNW', centerAngle: 292.5, range: [281.26, 303.75] },
    { direction: '西北', symbol: 'NW', centerAngle: 315, range: [303.76, 326.25] },
    { direction: '北西北', symbol: 'NNW', centerAngle: 337.5, range: [326.26, 348.75] }
]


const path = "/getWeatherData";
const getWeather = {
    //台风
    typhoon: () => {
        const params = { productType: "TYPHOON", code: 440700 };
        return request({ "url": path, params: params, 'methods': "GET" })
    },
    //实时天气
    current_weather: () => {
        const params = { productType: "CURRENT_WEATHER", code: 440700 };
        return request({ "url": path, params: params, 'methods': "GET" })
    },
    //天气预报
    forecast: () => {

        const params = { productType: "FORECAST", code: 440700 };
        return request({ "url": path, params: params, 'methods': "GET" })
    },
    //预警
    warning_info: () => {
        const params = { productType: "WARNING_INFO", code: 440700 };
        return request({ "url": path, params: params, 'methods': "GET" })
    }
}

/**
 * 
 * @param {Array} list 对应 气象的 f_rows https://open.gd121.cn/share/help.do 
 * @param {String} f_dt  "2023-10-13 08:00:00"
 * @returns []|null
 */
function due_forecast(list = [], f_dt) {
    if (!list) { return []; }
    var list2 = [];
    list.map((item, index) => {
        const ListIndex = Math.floor(index / 2);
        if (!list2[ListIndex]) {
            var addDate = dayjs(f_dt).add(ListIndex, 'day');
            const mmdd = (addDate.month() + 1) + "/" + addDate.date();
            list2[ListIndex] = {
                "wDay": null, 'night': null, 'dayObj': { text: weekdays[addDate.day()], index: addDate.day(), "mmdd": mmdd },
                "des": "day表示日间,night表示夜晚"
            }
        }
        var key = (index % 2) == 0 ? 'wDay' : 'night';
        for (let k in weathers) {
            if (weathers[k].description.indexOf(item.s) !== -1) {
                list2[ListIndex][key] = Object.assign(item, { "w_k": k }, weathers[k]);
                break;
            }
        }
    });

    return list2;
}


export { getWeather, due_forecast }